//
//  ZHomeModelViewModel.m
//  OC-weibo
//
//  Created by zero on 17/3/24.
//  Copyright © 2017年 zero. All rights reserved.
//

#import "ZHomeModelViewModel.h"

@implementation ZHomeModelViewModel


#pragma mark
#pragma mark - 重写时间的get方法
- (NSString *)timeString{
    
    return [self getTimerString:self.userModel.created_at];
}

#pragma mark
#pragma mark - 重写来源的get方法
- (NSString *)soucreString{
    
    return [self getSoucreString:self.userModel.source];
}

#pragma mark
#pragma mark - 来源 的处理
- (NSString *)getSoucreString:(NSString *)source{
    
    if (source.length == 0) {
        
        return @"遥远的地方";
    }
    
    NSString *startString = @"\">";
    NSString *endString = @"</";
    
    NSRange startRang = [source rangeOfString:startString];
    NSRange endRang = [source rangeOfString:endString];
    
    NSRange rang = NSMakeRange(startRang.location + startRang.length, endRang.location - startRang.location - startRang.length);
    NSString *targetString = [source substringWithRange:rang];

    
    return targetString;
}

#pragma mark
#pragma mark - 时间处理
- (NSString *)getTimerString:(NSDate *)date{
    
    BOOL res = [self isThisYear:date];
    
    //  时间格式化控制器
    NSDateFormatter *formtter = [[NSDateFormatter alloc] init];
    
    
    if (res) {
        
        NSCalendar *calendar = [NSCalendar currentCalendar];
        
        //  是不是今天
        if ([calendar isDateInToday:date]) {
            
            NSDate *nowDate = [NSDate date];
            
            NSInteger second = (NSInteger)[nowDate timeIntervalSinceDate:date];
            
            if (second < 60) {
                
                return @"刚刚";
            }else if (second < 60 * 60){
                
                return [NSString stringWithFormat:@"%zd分钟前",second/60];
            }else{
                
                return [NSString stringWithFormat:@"%zd小时前",second/3600];
            }
            
        }else if([calendar isDateInYesterday:date]){
            
            [formtter setDateFormat:@"昨天 hh时MM分"];
        }else{
            
            [formtter setDateFormat:@"mm月dd日 hh时MM分"];
        }
        
        
    }else{
        
        [formtter setDateFormat:@"yyyy年mm月dd日 hh时MM分"];
        
    }
    
    NSString *timesStr = [formtter stringFromDate:date];
    
    return timesStr;
}

#pragma mark
#pragma mark - 判断是够是今年
- (BOOL)isThisYear:(NSDate *)date{
    
    //  时间格式化控制器
    NSDateFormatter *formtter = [[NSDateFormatter alloc] init];
    
    //  设置格式化的样式
    [formtter setDateFormat:@"yyyy"];
    
    //  获取年份
    NSString *dateString = [formtter stringFromDate:date];
    
    //  获取当前年份
    NSString *sinceString = [formtter stringFromDate:[[NSDate alloc] init]];

    
    return [sinceString isEqualToString:dateString];
    
}
#pragma mark
#pragma mark - 重写set方法

- (void)setUserModel:(ZUserModel *)userModel{
    
    _userModel = userModel;
    
    [self getMbrank:userModel.user.mbrank];
    [self getVerified:userModel.user.verified];
    [self getProfileUrl:userModel.user.profile_image_url];
}

#pragma mark
#pragma mark - 得到头像的URL
- (void)getProfileUrl:(NSString *)profile{
    
    self.urlProfilr = [NSURL URLWithString:profile];
}

#pragma mark
#pragma mark - 认证
- (void)getVerified:(NSInteger)verified{
    
    switch (verified) {
            case 1:
            self.imgVerified = [UIImage imageNamed:@"avatar_vip"];
            break;
            case 2:
            case 3:
            case 5:
            self.imgVerified = [UIImage imageNamed:@"avatar_enterprise_vip"];
            break;
            case 220:
            self.imgVerified = [UIImage imageNamed:@"avatar_grassroot"];
            break;
            
        default:
            self.imgVerified = [UIImage imageNamed:@"avatar_vgirl"];
            break;
    }
}

#pragma mark
#pragma mark - 会员等级
- (void)getMbrank:(NSInteger)mbrank{
    
    if (mbrank == 0) {
        
        self.imgMbrank = [UIImage imageNamed:@"common_icon_membership_expired"];
    }else{
        
        NSString *imgString = [NSString stringWithFormat:@"common_icon_membership_level%ld",(long)mbrank];
        
        self.imgMbrank = [UIImage imageNamed:imgString];
    }
 
}

@end
